// pages/join/join.js
var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		currentTab: 0 ,// tab切换 
		companyObj:{},
		personalObj:{},
		invitationObj:{},

		mask: true,//组件alert
		animationData: {},//组件alert
		alertTitle: '提示',//组件alert
		alertPlaceholder: '请输入内容',//组件alert
		alertTip: '例如：输入一段文字',//组件alert
		alertTextareaVal: '',//组件alert
		element:'',//触发事件的节点

		companyLicense:'/images/small_avatar.png',//企业默认图片
		enterprisesCard: '/images/small_avatar.png'//个人企业名片
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.getApplyInfo();
	},

	//获取审核状态
	getApplyInfo: function(){
		var that = this;
		app.loading();
		//判断审核状态
		app.apiGet(app.apiList.getApplyInfo, {}, function (res) {
			app.hideloading();
			if (res.code = 200) {
				that.setData({
					'companyObj.companyCodeStatus': res.data.apply_enterprise,
					'companyObj.reason_enterprise': res.data.reason_enterprise,
					'personalObj.personalCodeStatus': res.data.apply_personal,
					'personalObj.reason_personal': res.data.reason_personal,
					'invitationObj.invitationCodeStatus': res.data.invite_code,
				})

			} else if (res.code = 1001) {
				//重新登录
			}
		})
	},

	//点击tab切换
	swichNav: function (e) {
		var that = this;
		if (this.data.currentTab === e.target.dataset.current) {
			return false;
		} else {
			this.setData({
				currentTab: e.target.dataset.current
			})
		}

	},
	
	//关闭 alert组件
	closeActionVal: function () {
		var animation = wx.createAnimation({
			duration: 200,
			timingFunction: 'linear',
		})
		animation.bottom('-640rpx').step()
		this.setData({
			mask: true,
			animationData: animation.export(),
			alertTextareaVal: ''
		})
	},
	//打开 alert组件
	showActionVal: function () {
		var animation = wx.createAnimation({
			duration: 200,
			timingFunction: 'linear'
		})
		animation.bottom('0').step()

		this.setData({
			mask: false,
			animationData: animation.export(),
		})

		//缓存
		if (this.data.companyObj.companySelf && this.data.element == 'companySelf') {
			this.setData({
				alertTextareaVal: this.data.companyObj.companySelf
			})
		} else if (this.data.companyObj.companyDetail && this.data.element == 'companyDetail') {
			this.setData({
				alertTextareaVal: this.data.companyObj.companyDetail
			})
		} else if (this.data.companyObj.companyAddress && this.data.element == 'companyAddress') {
			this.setData({
				alertTextareaVal: this.data.companyObj.companyAddress
			})
		}
	},
	//获取alert textarea里的值
	alertTextareaValTap: function (e) {
		this.data.alertTextareaVal = e.detail.value;
	},
	//确定 alert组件
	alertActionSureTap: function (e) {

		if (this.data.element == 'companySelf') {
			this.setData({
				'companyObj.companySelf': this.data.alertTextareaVal
			})
		} else if (this.data.element == 'companyDetail') {
			this.setData({
				'companyObj.companyDetail': this.data.alertTextareaVal
			})
		} else if (this.data.element == 'companyAddress') {
			this.setData({
				'companyObj.companyAddress': this.data.alertTextareaVal
			})
		}
		this.closeActionVal();
	},

	// 企业入驻 ＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊start
	//重新申请
	againSubmitCompanyTap: function(){
		this.setData({
			'companyObj.companyCodeStatus': 0
		})
	},

	companyNameTap: function(e){ //企业名称
		this.data.companyObj.companyName = e.detail.value
	},
	contactPeopleTap: function (e) { //职位联系人
		this.data.companyObj.contactPeople = e.detail.value
	},
	emailTap: function (e) { //工作邮箱
		this.data.companyObj.email = e.detail.value
	},
	phoneNumberTap: function (e) { //手机号码
		this.data.companyObj.phoneNumber = e.detail.value
	},
	uploadImgCompanyTap:function(e){//企业执照
		var that = this;
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function (res) {
				// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths[0];
				var tempFiles = res.tempFiles[0];
				//图片不能大于2M
				if (tempFiles.size >= 2097152){
					app.alert('图片大小不能超过2M！');
				}else{
					that.setData({
						companyLicense: tempFilePaths,
						companyLicenseStatus: true
					})
				}
			}
		})

	},
	companySelfTap: function (e) { //公司介绍
		//this.data.companyObj.companySelf = e.detail.value
		this.setData({
			alertTitle: '公司介绍',//组件alert
			alertPlaceholder: '请输入公司介绍内容',//组件alert
			alertTip: '例如：互联网财税知识服务社区，拥有全国10+专业财税 人员，拥有cpa注会资，拥有全国10+专业财税 人员，拥有cpa注会资',//组件alert
			element: 'companySelf',
		})
		this.showActionVal();
	},
	companyDetailTap: function (e) { //公司详情
		//this.data.companyObj.companyDetail = e.detail.value
		this.setData({
			alertTitle: '公司详情',//组件alert
			alertPlaceholder: '请输入公司详情内容',//组件alert
			alertTip: '例如：B+轮、30+人、成长快',//组件alert
			element: 'companyDetail',
		})
		this.showActionVal();
	},
	companyAddressTap: function (e) { //公司地址
		//this.data.companyObj.companyAddress = e.detail.value
		this.setData({
			alertTitle: '公司地址',//组件alert
			alertPlaceholder: '请输入公司地址内容',//组件alert
			alertTip: '例如：东城区首东国际b座1106',//组件alert
			element: 'companyAddress',
		})
		this.showActionVal();
		
	},
	submitCompanyTap: function(){
		var that = this;
		if (this.data.companyObj.companyName == undefined || this.data.companyObj.companyName == ''){
			app.alert('请填写企业名称');
			return false;
		}
		if (this.data.companyObj.contactPeople == undefined || this.data.companyObj.contactPeople == '') {
			app.alert('请填写职位联系人');
			return false;
		}
		if (this.data.companyObj.email == undefined || this.data.companyObj.email == '') {
			app.alert('请填写工作邮箱');
			return false;
		}
		if (this.data.companyObj.phoneNumber == undefined || this.data.companyObj.phoneNumber == '') {
			app.alert('请填写手机号码');
			return false;
		}
		if (!(/^1(3|4|5|7|8)\d{9}$/.test(this.data.companyObj.phoneNumber))) {
			app.alert('手机号码格式不对！');
			return false;
		} else if (!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(this.data.companyObj.email)) {
			app.alert('邮箱格式不对！');
			return false;
		}
		if (!this.data.companyLicenseStatus){
			app.alert('请上传企业执照');
			return false;
		}
		if (this.data.companyObj.companySelf == undefined || this.data.companyObj.companySelf == '') {
			app.alert('请填写公司介绍');
			return false;
		}
		if (this.data.companyObj.companyDetail == undefined || this.data.companyObj.companyDetail == '') {
			app.alert('请填写公司详情');
			return false;
		}
		if (this.data.companyObj.companyAddress == undefined || this.data.companyObj.companyAddress == '') {
			app.alert('请填写公司地址');
			return false;
		}
		app.loading();
		app.util.uploadImage(this.data.companyLicense,function(res){
			that.data.companyObj.companyLicense = res.data.path;
			app.apiPost(app.apiList.joinEnterprise, that.data.companyObj, function (res) {
				app.hideloading();
				if (res.code == 200) {
					wx.showToast({
						title: '提交成功！',
						icon: 'success',
						duration: 1000,
						mask: true,
						success: function(){
							that.setData({
								'companyObj.companyCodeStatus': 1,
							})
						}
					})

				}
			})
		});
		
	},
	// 企业入驻 ＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊end

	// 个人入驻 ＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊start
	//重新申请
	againSubmitPersonalTap: function(){
		this.setData({
			'personalObj.personalCodeStatus': 0
		})
	},

	userNameTap: function (e) { //姓名
		this.data.personalObj.userName = e.detail.value
	},
	userJobTap: function (e) { //工作职称
		this.data.personalObj.userJob = e.detail.value
	},
	userCompanyTap: function (e) { //所属公司
		this.data.personalObj.userCompany = e.detail.value
	},
	userPhoneNumTap: function (e) { //手机号码
		this.data.personalObj.userPhoneNum = e.detail.value
	},
	userWorkEmailTap: function (e) { //工作邮箱
		this.data.personalObj.userWorkEmail = e.detail.value
	},
	uploadImgUserTap: function(e){//个人企业名片
		var that = this;
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function (res) {
				// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths[0];
				var tempFiles = res.tempFiles[0];
				//图片不能大于2M
				if (tempFiles.size >= 2097152) {
					app.alert('图片大小不能超过2M！');
				} else {
					that.setData({
						enterprisesCard: tempFilePaths,
						enterprisesCardStatus: true
					})
				}
			}
		})
	},
	submitUserTap: function(){
		var that = this;
		if (this.data.personalObj.userName == undefined || this.data.personalObj.userName == '') {
			app.alert('请填写真实姓名');
			return false;
		}
		if (this.data.personalObj.userJob == undefined || this.data.personalObj.userJob == '') {
			app.alert('请填写工作职称');
			return false;
		}
		if (this.data.personalObj.userCompany == undefined || this.data.personalObj.userCompany == '') {
			app.alert('请填写所属公司');
			return false;
		}
		if (this.data.personalObj.userPhoneNum == undefined || this.data.personalObj.userPhoneNum == '') {
			app.alert('请填写手机号码');
			return false;
		}
		if (this.data.personalObj.userWorkEmail == undefined || this.data.personalObj.userWorkEmail == '') {
			app.alert('请填写工作邮箱');
			return false;
		}
		if (!this.data.enterprisesCardStatus) {
			app.alert('请上传企业名片');
			return false;
		}

		app.loading();
		var serverImg = app.util.uploadImage(this.data.enterprisesCard,function(res){
			that.data.personalObj.enterprisesCard = res.data.path;
			app.apiPost(app.apiList.joinPersonal, that.data.personalObj, function (res) {
				app.hideloading();
				if (res.code == 200) {
					wx.showToast({
						title: '提交成功！',
						icon: 'success',
						duration: 1000,
						mask: true,
						success: function () {
							that.setData({
								'personalObj.personalCodeStatus': 1,
							})
						}
					})
				}
			})
		});
	},
	// 个人入驻 ＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊end

	// 内部邀请码 ＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊start
	invitationCodeTap: function(e){
		this.data.invitationObj.invitationCode = e.detail.value
	},
	submitInvitationCodeTap: function(){
		if (this.data.invitationObj.invitationCode == undefined || this.data.invitationObj.invitationCode == '') {
			app.alert('请填写邀请码资料');
			return false;
		}
		app.loading();
		var dataJson = {
			code: this.data.invitationObj.invitationCode
		}
		app.apiPost(app.apiList.joinInvitation, dataJson , function (res) {
			app.hideloading();
			if (res.code == 200) {
				wx.showToast({
					title: '提交成功！',
					icon: 'success',
					duration: 1000,
					mask: true,
					success: function () {
						that.setData({
							'invitationObj.invitationCodeStatus': 1,
						})
					}
				})
			} else if (res.code == 202){
				app.alert('未找到验证码！');
			}else{
				app.alert(res.message);
			}
		})
	},
	// 内部邀请码 ＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊end


	//回到我的页面
	sureBackTap: function(){
		wx.navigateBack({
			
		})
	} ,
	//share 统一分享
	onShareAppMessage: function (res) {
		if (res.from === 'button') {
			// 来自页面内转发按钮
			console.log(res.target)
		}
		return {
			title: '财咖｜会计招聘找工作',
			path: '/pages/index/index',
			imageUrl: '/images/share.jpg',
			success: function (res) {
				// 转发成功
			},
			fail: function (res) {
				// 转发失败
			}
		}
	},
})